//
//  YAYIPatientComeCell.m
//  YAYIDoctor
//
//  Created by hxp on 16/4/29.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIPatientComeCell.h"

@implementation YAYIPatientComeCell

- (void)awakeFromNib {
    [super awakeFromNib];
  
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

   
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createCustomView];
    }
    return self;
}

-(void)setModel:(YAYIPatientModel *)model
{
    _model = model;
    [self reloadData];
}


-(void)reloadData
{
    self.name.text =   [self infor:_model.patientname gender:_model.gender age:[self ageStr:_model.birthday]];
    self.message.text = _model.remark;
    self.dateLab.text = [NSString dateString:_model.createtime];
  

}

-(NSString *)ageStr:(NSString *)age
{
    NSString *birthdateString = [NSString dateString:age];
    
    int  birth = [[birthdateString  substringToIndex:4] intValue];
    NSDate *date = [NSDate new];
    NSDateFormatter *formater = [NSDateFormatter new];
    formater.dateFormat = @"YYYY";
    NSString *dateStr = [formater  stringFromDate:date];
    int curDate = [dateStr intValue];
    return [NSString stringWithFormat:@"%d",(curDate - birth)];

}

-(NSString *)infor:(NSString *)name gender:(NSString *)gender age:(NSString *)age
{
    NSString *gen = [NSString stringWithFormat:@"%@",gender];
    
    if (self.model.idcard && ![self.model.idcard isKindOfClass:[NSNull class]]){
        
        if ([gen isEqualToString:@"-1"]) {
            return [NSString stringWithFormat:@"%@  %@  %@",name,@"女",age];
        }else{
            return [NSString stringWithFormat:@"%@  %@  %@",name,@"男",age];
        }
        
    }else{
    
        return [NSString stringWithFormat:@"%@", name];
        
    }
    
    
}
-(void)createCustomView
{
    self.name = [UILabel new];
    self.name.font = [UIFont systemFontOfSize:font16];
    self.name.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.name];
    self.message = [UILabel new];
    self.message.numberOfLines = 1;
    self.message.font = [UIFont systemFontOfSize:font14];
    self.message.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.message];
    self.dateLab = [UILabel new];
    self.dateLab.font = [UIFont systemFontOfSize:font12];
    self.dateLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.dateLab];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
   
    self.button.titleLabel.font = [UIFont systemFontOfSize:font14];
    self.button.clipsToBounds = YES;
    self.button.layer.cornerRadius = 3;
    [self.button.titleLabel setTextColor:[UIColor whiteColor]];
    [self.contentView addSubview:self.button];
    self.seprateLine = [UILabel new];
    self.seprateLine.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    [self.contentView addSubview:self.seprateLine];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(12));
        make.left.equalTo(@(30));
        make.right.mas_equalTo(@(-78));
    }];
    
    [self.message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.name.mas_left);
        make.top.mas_equalTo(self.name.mas_bottom).with.offset(8);
        make.right.equalTo(@(-78));
    }];
    
    [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-12);
        make.left.mas_equalTo(self.name.mas_left);
        make.size.mas_equalTo(CGSizeMake(160, 14));
    }];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(-10);
        make.size.mas_equalTo(CGSizeMake(70, 34));
    }];
    
    [self.seprateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.bottom.mas_equalTo(self.contentView).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, 0.5));
    }];
}
@end
